package com.moli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.constans.SystemConstants;
import com.moli.domain.entity.Menu;
import com.moli.mapper.MenuMapper;
import com.moli.service.MenuService;
import com.moli.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author： XC
 * @Date： 2023-06-08 14:11
 * @Description： 菜单权限表(Menu)表服务实现类
 */

@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

	@Override
	public List<String> selectPermsByUserId(Long id) {
		//1.如果是管理员，返回所有的权限
		if (SecurityUtils.isAdmin()) {
			LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
			//查询菜单类型中的菜单和按钮
			wrapper.in(Menu::getMenuType, SystemConstants.MENU, SystemConstants.BUTTON);
			//查询菜单状态正常的信息
			wrapper.eq(Menu::getStatus, SystemConstants.STATUS_NORMAL);
			List<Menu> menus = list(wrapper);
			List<String> perms = menus.stream()
					.map(Menu::getPerms)
					.collect(Collectors.toList());
			return perms;
		}
		//2.不是管理员的话，返回该用户所具有的权限
		//根据用户id查角色id，然后根据角色id查菜单id，再根据菜单id查询perms（权限标识）字段即可
		return getBaseMapper().selectPermsByUserId(id);
	}

	@Override
	public List<Menu> selectRouterMenuTreeByUserId(Long userId) {
		MenuMapper menuMapper = getBaseMapper();
		List<Menu> menus = null;
		//1.判断是否是管理员
		if (SecurityUtils.isAdmin()) {
			//2.如果是 获取所有符合要求的Menu
			menus = menuMapper.selectAllRouterMenu();
		} else {
			//3.否则  获取当前用户所具有的Menu
			menus = menuMapper.selectRouterMenuTreeByUserId(userId);
		}

		//构建tree
		//先找出第一层的菜单  然后去找他们的子菜单设置到children属性中  递归思想
		List<Menu> menuTree = builderMenuTree(menus, 0L);
		return menuTree;
	}

	@Override
	public List<Menu> selectMenuList(Menu menu) {

		LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
		//menuName模糊查询
		queryWrapper.like(StringUtils.hasText(menu.getMenuName()), Menu::getMenuName, menu.getMenuName());
		queryWrapper.eq(StringUtils.hasText(menu.getStatus()), Menu::getStatus, menu.getStatus());
		//排序 parent_id和order_num
		queryWrapper.orderByAsc(Menu::getParentId, Menu::getOrderNum);
		List<Menu> menus = list(queryWrapper);
		;
		return menus;
	}

	@Override
	public boolean hasChild(Long menuId) {
		LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Menu::getParentId, menuId);
		return count(queryWrapper) != 0;
	}

	@Override
	public List<Long> selectMenuListByRoleId(Long roleId) {
		return getBaseMapper().selectMenuListByRoleId(roleId);
	}

	private List<Menu> builderMenuTree(List<Menu> menus, Long parentId) {
		List<Menu> menuTree = menus.stream()
				//过滤处理 匹配对应的父菜单ID
				.filter(menu -> menu.getParentId().equals(parentId))
				.map(menu -> menu.setChildren(getChildren(menu, menus)))
				.collect(Collectors.toList());
		return menuTree;
	}

	/**
	 * 获取存入参数的 子Menu集合
	 *
	 * @param menu
	 * @param menus
	 * @return
	 */
	private List<Menu> getChildren(Menu menu, List<Menu> menus) {
		List<Menu> childrenList = menus.stream()
				.filter(m -> m.getParentId().equals(menu.getId()))
				.map(m -> m.setChildren(getChildren(m, menus)))
				.collect(Collectors.toList());
		return childrenList;
	}
}